﻿using AutoMapper;
using MediatR;
using S.BreedingHistory.Domain.Model;
using S.BreedingHistory.Infrastructure.Interface;
using S.BreedingHistory.Read.Api.Application.Command;
using S.BreedingHistory.Read.Api.Dto;

namespace S.BreedingHistory.Read.Api.Application.CommandHandler
{
    public class BreedingHistoryByEarTagCommandHandler : IRequestHandler<BreedingHistoryByEarTagCommand, BreedingHistoryListDto>
    {
        private readonly IBreedingHistoryRepository breedingHistoryRepository;
        private readonly IMapper mapper;

        public BreedingHistoryByEarTagCommandHandler(IBreedingHistoryRepository breedingHistoryRepository,IMapper mapper)
        {
            this.breedingHistoryRepository = breedingHistoryRepository;
            this.mapper = mapper;
        }
        /// <summary>
        /// 通过种母耳号获取最新的记录
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public async Task<BreedingHistoryListDto> Handle(BreedingHistoryByEarTagCommand request, CancellationToken cancellationToken)
        {
            BreedingHistoryModel breedingHistory=await breedingHistoryRepository.GetNewBreedingHistoryByEarTag(request.EarTag);
            BreedingHistoryListDto breedingHistoryListDto = mapper.Map<BreedingHistoryListDto>(breedingHistory);
            return breedingHistoryListDto;
        }
    }
}
